package linked;

/**
 * @author
 * @date 2025/4/15 19:51
 */
public class 链表反转 {
    public static void main(String[] args) {
        LinkedNode header = new LinkedNode("01");
        LinkedList linkedList = new LinkedList(header);
        linkedList.add(new LinkedNode("02"));
        linkedList.add(new LinkedNode("03"));
        linkedList.add(new LinkedNode("04"));
        linkedList.add(new LinkedNode("05"));
        linkedList.print();

        System.out.println();
        reverseLinked(linkedList);
        linkedList.print();
    }

    /**
     * 双指针+在反转前保存cur.next，防止在反转后拿不到next
     * @param
     */
    public static void reverseLinked(LinkedList linkedList){
        LinkedNode node  = linkedList.header;
        LinkedNode pre = null;
        LinkedNode cur = node;
        // 先记录当前节点的下一个节点
        while (cur!=null){
            linkedList.header = cur;
            LinkedNode temp = cur.next;
            cur.next = pre;
            pre = cur;
            cur = temp;
        }

    }
}
